﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Xml;

namespace XML_Programming_Assignment_Thandar_
{
    public partial class ReadRFQInfo : Form
    {
        public ReadRFQInfo()
        {
            InitializeComponent();
        }

        private void btnRead_Click(object sender, EventArgs e)
        {
            string FilePath = "";
            OpenFileDialog.Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*";
            OpenFileDialog.InitialDirectory = "C:";
            OpenFileDialog.Title = "Select an RFQ";

            if (OpenFileDialog.ShowDialog() == DialogResult.OK)
            {
                FilePath = OpenFileDialog.FileName;
                tbxFilePath.Text = FilePath;
                string points = "";
                ArrayList RFQDetailList = Class.DBManager.ReadXML(FilePath);
                XmlReader reader = XmlReader.Create(FilePath);
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        points = reader.Name.ToString();
                        
                    }
                    if (reader.NodeType == XmlNodeType.Text)
                    {
                        tbxRFQDetail.Text += points + ": " + reader.Value + "\r\n";
                    }

                    if (reader.HasAttributes)
                    {
                        while (reader.MoveToNextAttribute())
                        {
                            if (reader.Name.Equals("version") || reader.Name.Equals("encoding") || reader.Name.Equals("xsi:noNamespaceSchemaLocation") || reader.Name.Equals("xmlns:xsi"))
                            {

                            }
                            else
                            {
                                tbxRFQDetail.Text += "\r\n"+reader.Name + ": " + reader.Value + "\r\n";
                            }
                        }
                    }
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Class.DBManager.SaveRFQ();
            tbxFilePath.Clear();
            tbxRFQDetail.Clear();
        }
             
        private void homeToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            this.Hide();
            Class.AllFormClass.form_default.Show();
        }

        private void readRFQToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            this.Hide();
            Class.AllFormClass.form_readRFQ.Show();
        }

        private void generateQuotationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Hide();
            Class.AllFormClass.form_quotation.Show();
        }
    }
}
